//
//  SRDay+Cate.m
//  SportRing
//
//  Created by mykoma on 14/10/27.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRDay+Cate.h"
#import "SRDB.h"

@implementation SRDay (Cate)

+ (SRDay *)day
{
    __block SRDay * day = nil;
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         day = [SRDay MR_createEntity];
     }];
    return day;
}

- (void)configAttributesWithBlock:(void(^)(SRDay * aSelf))block
{
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         block(self);
     }];
}

@end

@implementation SRDay (Find)

+ (NSArray *)findDaysForMonth:(SRMonth *)month
{
    __block NSMutableArray * mArray = [@[] mutableCopy];
    [month.days enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        SRDay * day = obj;
        if ([day countOfSteps] > 0) {
            [mArray addObject:obj];
        }
    }];
    
    return [mArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        SRDay * day1 = obj1;
        SRDay * day2 = obj2;
        return [day1.createDate compare:day2.createDate];
    }];
}

+ (SRDay *)findDayForDate:(NSDate *)date forMe:(SRMe *)me
{
    NSString * dateStr = [date formattedBy_yyyyMMdd];
    return [SRDay findDayForDateStr:dateStr forMe:me];
}

+ (SRDay *)findDayForDateStr:(NSString *)dateStr forMe:(SRMe *)me
{
    NSPredicate * predicate = [NSPredicate predicateWithFormat:@"createDate == %@ AND me.userName == %@",
                               dateStr, me.userName];
    return [SRDay MR_findFirstWithPredicate:predicate];
}

+ (SRDay *)findDayCINEForDate:(NSDate *)date forMe:(SRMe *)me
{
    NSString * dateStr = [date formattedBy_yyyyMMdd];
    return [SRDay findDayCINEForDateStr:dateStr forMe:me];
}

+ (SRDay *)findDayCINEForDateStr:(NSString *)dateStr forMe:(SRMe *)me
{
    SRDay * day = [SRDay findDayForDateStr:dateStr forMe:me];
    if (day == nil) {
        day = [SRDay day];
        [day configAttributesWithBlock:^(SRDay *aSelf) {
            NSDate * date = [NSDate dateFromString:dateStr format:nil];
            SRMonth * month = [SRMonth findMonthCINEForMonthStr:[date formattedBy_yyyyMM]
                                                          forMe:me];
            aSelf.month = month;
            aSelf.me = me;
            aSelf.createDate = dateStr;
        }];
    }
    return day;
}

- (NSInteger)countOfSteps
{
    __block NSInteger count = 0;
    
    [self.sportParts enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        SRSportPart * part = obj;
        count += part.walkCounter.intValue + part.runCounter.intValue + part.upstairsCounter.intValue;
    }];
    
    if (self.counter.intValue > count) {
        count = self.counter.intValue;
    }
    
    self.counter = @(count);
    
    return count;
}

- (CGFloat)countOfDistance
{
    __block NSInteger count = 0;
    
    [self.sportParts enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        SRSportPart * part = obj;
        count += part.mileage.floatValue;
    }];
    
    if (self.mileage.floatValue > count)
    {
        count = self.mileage.floatValue;
    }
    
    self.mileage = @(count);
    
    return count;
}

- (CGFloat)countOfCalorie
{
    __block NSInteger count = 0;
    
    [self.sportParts enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        SRSportPart * part = obj;
        count += part.calorie.floatValue;
    }];
    
    if (self.calorie.floatValue > count) {
        count = self.calorie.floatValue;
    }
    
    self.calorie = @(count);
    
    return count;
}

- (CGFloat)averageTemperature
{
    __block CGFloat all = 0;
    
    [self.sportParts enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        SRSportPart * part = obj;
        all += part.t1.floatValue + part.t2.floatValue + part.t3.floatValue + part.t4.floatValue;
    }];
    
    return all == 0 ? 0 : all / (4 * self.sportParts.count);
}

- (CGFloat)altitude
{
    __block CGFloat all = 0;
    
    [self.sportParts enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        
    }];
    for (id obj in self.sportParts) {
        SRSportPart * part = obj;
        all += part.p1.floatValue;
    }
    
    
    CGFloat average = all == 0 ? 0 : all / self.sportParts.count;
    if (average == 0) {
        return 0;
    }
    else
    {
        return 44330.0 *(1.0 - pow(average / 101.325, 0.1903));
    }
}

@end